using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.Business.Dto;
using ZR.Model.Business;
using ZR.Repository;
using ZR.Service.Business.IBusinessService;

namespace ZR.Service.Business
{
    /// <summary>
    /// 用户表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IUserService), ServiceLifetime = LifeTime.Transient)]
    public class UserService : BaseService<User>, IUserService
    {
        /// <summary>
        /// 查询用户表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<UserDto> GetList(UserQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<User, UserDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public User GetInfo(long UserId)
        {
            var response = Queryable()
                .Where(x => x.UserId == UserId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加用户表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public User AddUser(User model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改用户表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateUser(User model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<User> QueryExp(UserQueryDto parm)
        {
            var predicate = Expressionable.Create<User>();

            return predicate;
        }
    }
}